package suncertify.db.test;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test2 {

    private final static Lock lock = new ReentrantLock();
    private final static Condition lockCondition = lock.newCondition();

    private static long cookie = -1;

    public static void main(String[] args) {

	Thread databaseThread = new Thread() {

	    public void run() {
		System.out.println("DatabaseThread Before lock();");
		lock.lock();
		System.out.println("DatabaseThread After lock();");

		try {
		    File file = new File("C:\\Infor\\Expert_WMWares\\DEDAVWPerf2k8b.zip");
		    RandomAccessFile raf = new RandomAccessFile(file, "rw");

		    // Thread.currentThread().sleep(7000);
		    for (int i = 0; i < 7000000; ++i) {
			byte b = raf.readByte();
		    }
		    raf.close();

		    System.out.println("DatabaseThread After sleep;");
		    cookie = 100;
		    lockCondition.signalAll();
		} catch (Exception e) {
		} finally {
		    lock.unlock();
		}
	    }
	};

	lock.lock();

	try {
	    databaseThread.start();
	    System.out.println("MainThread after databaseThread.start() and before await;");
	    long t1 = System.nanoTime();
	    lockCondition.await(2, TimeUnit.SECONDS);
	    long t2 = System.nanoTime() - t1;
	    System.out.println("MainThread after await; " + Long.toString(t2 / 1000000L));

	} catch (InterruptedException e) {
	    System.out.println("MainThread InterruptedException!!!");

	} finally {
	    lock.unlock();
	}
    }
}
